﻿// -----------------------------------------------------------------------
// <copyright file="ProfileCompositionProfileListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProfileCompositionProfileList
    /// </summary>
    public class ProfileCompositionProfileListData
    {
        /// <summary>
        /// Get ProfileCompositionProfileList By Id
        /// </summary>
        /// <param name="profileCompositionProfileListId">Param profileCompositionProfileListId</param>
        /// <returns>Return ProfileCompositionProfileList</returns>
        public ProfileCompositionProfileListEN GetProfileCompositionProfileListById(int profileCompositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileCompositionProfileListEN profileCompositionProfileList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListGetById, profileCompositionProfileListId);
                while (reader.Read())
                {
                    profileCompositionProfileList = new ProfileCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileCompositionProfileList.ProfileCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileCompositionProfileList.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileCompositionProfileList
        /// </summary>
        /// <returns>Return list all ProfileCompositionProfileList</returns>
        public IList<ProfileCompositionProfileListEN> GetProfileCompositionProfileLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileCompositionProfileListEN> lstProfileCompositionProfileList = new List<ProfileCompositionProfileListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListGetAll);
                while (reader.Read())
                {
                    ProfileCompositionProfileListEN profileCompositionProfileList = new ProfileCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileCompositionProfileList.ProfileCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileCompositionProfileList.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }

                    lstProfileCompositionProfileList.Add(profileCompositionProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileCompositionProfileList
        /// </summary>
        /// <param name="profileCompositionProfileList">Paarm ProfileCompositionProfileList</param>
        /// <returns>Insert ProfileCompositionProfileList</returns>
        public int Insert(ProfileCompositionProfileListEN profileCompositionProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.ProfileId, profileCompositionProfileList.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.CompositionProfileListId, profileCompositionProfileList.CompositionProfileListId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileCompositionProfileList
        /// </summary>
        /// <param name="profileCompositionProfileList">Param ProfileCompositionProfileList</param>
        public void Update(ProfileCompositionProfileListEN profileCompositionProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.ProfileCompositionProfileListId, profileCompositionProfileList.ProfileCompositionProfileListId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.ProfileId, profileCompositionProfileList.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.CompositionProfileListId, profileCompositionProfileList.CompositionProfileListId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileCompositionProfileList by profileCompositionProfileListId
        /// </summary>
        /// <param name="profileCompositionProfileListId">param profileCompositionProfileListId</param>
        public void Delete(int profileCompositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.ProfileCompositionProfileListId, profileCompositionProfileListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileCompositionProfileList By ProfileId
        /// </summary>
        /// <param name="profileId">Param profileCompositionProfileListId</param>
        /// <returns>Return List ProfileCompositionProfileList</returns>
        public IList<ProfileCompositionProfileListEN> GetProfileCompositionProfileListByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileCompositionProfileListEN> lstProfileCompositionProfileList = new List<ProfileCompositionProfileListEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListGetByProfileId, profileId);
                while (reader.Read())
                {
                    ProfileCompositionProfileListEN profileCompositionProfileList = new ProfileCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileCompositionProfileList.ProfileCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileCompositionProfileList.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }

                    lstProfileCompositionProfileList.Add(profileCompositionProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Deletes the by profile id.
        /// </summary>
        /// <param name="profileId">The profile id.</param>
        public void DeleteByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileCompositionProfileList.ProfileId, profileId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileCompositionProfileListDeleteByProfileId, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}